package com.starpl.mobile.android.monthunsae.november.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.starpl.mobile.android.monthunsae.november.R;
import com.starpl.mobile.android.monthunsae.november.domain.MonthUnsae;
import com.starpl.mobile.android.monthunsae.november.domain.UserInfo;
import com.starpl.mobile.android.platform.command.StarplCommand;
import com.starpl.mobile.android.platform.common.DateUtil;
import com.starpl.mobile.android.platform.common.ads.AdBannerOnClickListener;
import com.starpl.mobile.android.platform.common.base.BaseActivity;
import com.starpl.mobile.android.platform.common.base.BaseApplication;
import com.starpl.mobile.android.platform.common.image.ImageDownloader;
import com.starpl.mobile.android.platform.common.network.HttpHandler;


/**
 * 월별 운세 상세 정보를 보여주는 Activity
 * 
 * @author 우연화
 * @since 2011.03.17
 */
public class UnsaeDetailActivity extends BaseActivity
{
	private final int INTRODUCTION 	= 0;
	private final int LUCKY_DAY		= 1;
	private final int PRESCRIPTION	= 2;
	private final int HEALTH		= 3;
	private final int BUSINESS		= 4;
	
	private TextView mTvUnsaeTitle;
	private ImageView mIvUnsaeIcon;
	private TextView mTvUserName;
	private TextView mTvUserBirthDay;
	private TextView mTvUnsaeInfo;
	private ImageView mIvBanner;
	private FrameLayout mFlNbizBanner;
	private TextView mTvNbizTitle;
	private TextView mTvNbizDesc;
	private ImageView mIvNbizBanner;
	private LinearLayout mLlCaulyBanner;
	
	/* (non-Javadoc)
	 * @see com.starpl.platform.common.base.BaseActivity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.unsae_detail_activity);
		
		mTvUnsaeTitle = (TextView)findViewById(R.id.unsae_detail_activity_tv_title);
		mIvUnsaeIcon = (ImageView)findViewById(R.id.unsae_detail_activity_iv_icon);
		mTvUserName = (TextView)findViewById(R.id.unsae_detail_activity_tv_user_name);
		mTvUserBirthDay = (TextView)findViewById(R.id.unsae_detail_activity_tv_birthday);
		mTvUnsaeInfo = (TextView)findViewById(R.id.unsae_detail_activity_tv_unsae_info);
		mIvBanner = (ImageView) findViewById(R.id.unsae_detail_activity_iv_banner);
		mLlCaulyBanner = (LinearLayout) findViewById(R.id.unsae_detail_activity_cauly_banner);
		
		// NBIZ 광고
		mFlNbizBanner = (FrameLayout) findViewById(R.id.unsae_detail_activity_nbiz_banner);
		mTvNbizTitle = (TextView) findViewById(R.id.nbiz_ad_banner_tv_nbiz_title);
		mTvNbizDesc = (TextView) findViewById(R.id.nbiz_ad_banner_tv_nbiz_description);
		mIvNbizBanner = (ImageView) findViewById(R.id.nbiz_ad_banner_iv_banner);
		
		int unsaeType = getIntent().getExtras().getInt("unsaeType");
		MonthUnsae monthUnsae = (MonthUnsae)BaseApplication.getGlobalPersistent("monthUnsae");
		UserInfo userInfo = (UserInfo)BaseApplication.getGlobalPersistent("userInfo");
		String unsaeInfo = getUnsaeInformation(unsaeType, monthUnsae);
		setUserName(userInfo.getUserName(), monthUnsae.getUnsaeDate());
		setUserBirthDay(userInfo.getBirthDate(), userInfo.getLunarType());
		setUnsaeInformation(unsaeInfo);
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		
		TelephonyManager telManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
		String deviceId = telManager.getDeviceId();
		
		StarplCommand.getCommonAd(new HttpHandler(this)
		{
			@Override
			public void didSucceed(Object result)
			{
				try
				{
					JSONObject object = (JSONObject) result;
					boolean success = object.getBoolean("success");
					
					// 광고데이터가 정상적으로 날라왔을 경우
					if(success && ! object.isNull("extData3"))
					{
						JSONObject extData3 = object.getJSONObject("extData3");
						int adIdx = extData3.getInt("adIdx");
						
						if(!extData3.isNull("adType"))
						{
							String adType = extData3.getString("adType");
							
							// NBIZ 광고일 경우
							if(adType.equals("ADTC51"))
							{
								JSONObject adData = (JSONObject) extData3.getJSONObject("adData")
															.getJSONObject("zetadoc")
															.getJSONObject("ads")
															.getJSONArray("ad")
															.get(0);
								String title = adData.getString("title");
								String description = adData.getString("description");
								String url = adData.getString("landingUrl");
								String image = "http://shared.starpl.com/images2/ad/mobile/ad_back_android_480_78.png";
								
								mFlNbizBanner.setVisibility(LinearLayout.VISIBLE);
								mFlNbizBanner.setOnClickListener(new AdBannerOnClickListener(url, adIdx));
								mIvBanner.setVisibility(ImageView.GONE);
								mLlCaulyBanner.setVisibility(LinearLayout.GONE);
								mTvNbizTitle.setText(title);
								mTvNbizDesc.setText(description);
								ImageDownloader.download(image, mIvNbizBanner);
							}
							// Cauly 광고일 경우
							else if(adType.equals("ADTC52"))
							{
								AdInfo info = new AdInfo();
								info.initData(BaseApplication.getConfigPersistent("caulyCode"), "CPC", "all", "all", "off", "default", "yes", 90, true);
								
								AdView adView = new AdView(getParentContext());
								adView.setAdListener(new AdListener()
								{
									@Override
									public void onReceiveAd() {}
									
									@Override
									public void onFailedToReceiveAd(boolean arg0) {}
								});
								
								if(mLlCaulyBanner.getChildCount() > 0)
								{
									mLlCaulyBanner.removeAllViews();
								}
								mLlCaulyBanner.addView(adView);
								mLlCaulyBanner.setVisibility(LinearLayout.VISIBLE);
								mFlNbizBanner.setVisibility(LinearLayout.GONE);
								mIvBanner.setVisibility(ImageView.GONE);
							}
							else
							{
								String url = extData3.getString("url");
								String image = extData3.getString("image");
								
								mIvBanner.setVisibility(ImageView.VISIBLE);
								mLlCaulyBanner.setVisibility(LinearLayout.GONE);
								mFlNbizBanner.setVisibility(LinearLayout.GONE);
								mIvBanner.setOnClickListener(new AdBannerOnClickListener(url, adIdx));
								ImageDownloader.download(image, mIvBanner);
							}
						}
					}
				}
				catch(JSONException e)
				{
					LOG.e(e.getMessage());
				}
				
			}
		}, deviceId);
	}

	private String getUnsaeInformation(int unsaeType, MonthUnsae monthUnsae)
	{
		switch(unsaeType)
		{
		// 총론
		case INTRODUCTION:
			mTvUnsaeTitle.setText("총론");
			mIvUnsaeIcon.setBackgroundResource(R.drawable.img_fortune_01);
			return monthUnsae.getIntroduction();
		case LUCKY_DAY:
			mTvUnsaeTitle.setText("길일");
			mIvUnsaeIcon.setBackgroundResource(R.drawable.img_fortune_02);
			return monthUnsae.getLuckyDay();
		case PRESCRIPTION:
			mTvUnsaeTitle.setText("처방전");
			mIvUnsaeIcon.setBackgroundResource(R.drawable.img_fortune_03);
			return monthUnsae.getPrescription();
		case HEALTH:
			mTvUnsaeTitle.setText("건강운");
			mIvUnsaeIcon.setBackgroundResource(R.drawable.img_fortune_04);
			return monthUnsae.getHealth();
		case BUSINESS:
			mTvUnsaeTitle.setText("영업/거래/매매운");
			mIvUnsaeIcon.setBackgroundResource(R.drawable.img_fortune_05);
			return monthUnsae.getBusiness();
		default :
			return "";
		}
	}
	
	/**
	 * 화면에 유저의 이름을 세팅한다.
	 * 
	 * @param userName
	 */
	private void setUserName(String userName, String date)
	{
		date = date.substring(5, 7);
		mTvUserName.setText(userName + "님의 " + Integer.parseInt(date) + "월운세");
	}
	
	/**
	 * 화면에 유저의 생년월일을 세팅한다.
	 * 
	 * @param birthDay
	 * @param lunarType
	 */
	private void setUserBirthDay(String birthDay, int lunarType)
	{
		String lunarTypeString = "";
		
		switch(lunarType)
		{
		// 양력
		case 0 :
			lunarTypeString = "(양)";
			break;
		// 음력평달
		case 1 :
			lunarTypeString = "(음)";
			break;
		// 음력윤달
		case 2 :
			lunarTypeString = "(음력 윤달)";
			break;
		}
		DateUtil dateUtil = new DateUtil(birthDay);
		mTvUserBirthDay.setText(dateUtil.toStringByFormat("yyyy년 M월 d일") + lunarTypeString);
	}
	
	/**
	 * 화면에 유저의 이번달 해당 운세 정보를 세팅한다.
	 * 
	 * @param information
	 */
	private void setUnsaeInformation(String information)
	{
		mTvUnsaeInfo.setText(information);
	}
}
